package com.abl.service.system.impl;

import com.abl.common.mybatis.dao.Dao;
import com.abl.common.mybatis.model.Dnt;
import com.abl.common.mybatis.strengthen.Wrapper;
import com.abl.domain.model.AblError;
import com.abl.domain.model.Param;
import com.abl.common.util.StringKit;
import com.abl.service.system.SysPostService;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 资源表实现
 */
@Slf4j
@Service
public class SysPostServiceImpl implements SysPostService {
    @Autowired
    private Dao dao;

    //新增
    @Override
    public long add(Param<JSONObject> param) {
        //校验业务重复
        String name = param.getString("name");
        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.eq("name", name);
        long sys_post = dao.count("sys_post", qw);
        if (sys_post > 0) throw new AblError("操作失败：【职位名称:{0}】已经存在", name);


        String code = param.getString("code");
        qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.eq("code", code);
        long sys_post1 = dao.count("sys_post", qw);
        if (sys_post1 > 0) throw new AblError("操作失败：【职位编码:{0}】已经存在", code);

        Dnt dnt = Dnt.createInsert(param);

        return dao.insert("sys_post", dnt);
    }

    //删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean del(Param<JSONObject> param) {
        ArrayList<Object> ids = param.getList("ids");
        Wrapper<Object> uw = new Wrapper<>();
        uw.in("id", ids);
        uw.set("d_s", "1");
        dao.update("sys_post", uw);
        return true;
    }

    //修改
    @Override
    public long edit(Param<JSONObject> param) {
        //校验业务重复
        String id = param.getString("id");
        String name = param.getString("name");
        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.eq("name", name);
        qw.ne("id", id);
        long sys_post = dao.count("sys_post", qw);
        if (sys_post > 0) throw new AblError("操作失败：【职位名称:{0}】已经存在", name);


        String code = param.getString("code");
        qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.eq("code", code);
        qw.ne("id", id);
        long sys_post1 = dao.count("sys_post", qw);
        if (sys_post1 > 0) throw new AblError("操作失败：【职位编码:{0}】已经存在", code);

        Dnt dnt = Dnt.createUpdate(param);

        return dao.updateById("sys_post", dnt);
    }

    //查询详情
    @Override
    public Dnt info(Param<JSONObject> param) {
        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.eq("id", param.getOrDefault("id", 0));
        return dao.selectOne("sys_post", wrapper);
    }

    //查询详情
    @Override
    // @Cacheable(value = "ref_info", key = "#path")
    public Dnt info(String path) {
        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.eq("d_s", 0);
        wrapper.eq("path", path);
        return dao.selectOne("sys_post", wrapper);
    }

    //查询集合
    @Override
    public List<Dnt> list(Param<JSONObject> param) {
        var name = param.getString("name");
        var status = param.getString("status");
        var code = param.getString("code");

        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.eq(StringKit.isNotBlank(param.getString("id")), "id", param.getString("id"));
        qw.eq(StringKit.isNotBlank(code), "code", code);
        qw.like(StringKit.isNotBlank(name), "name", name);
        qw.like(StringKit.isNotBlank(status), "status", status);
        qw.orderByAsc("sort");
        if (param.isPage()) {
            return dao.page("sys_post", qw, param.pageCurrent(), param.pageSize());
        } else {
            return dao.list("sys_post", qw);
        }
    }

    //模糊查询条件下拉列表
    @Override
    public List<Dnt> searchInput(Param<JSONObject> param) {
        String sys_name = param.getString("name", "");
        String sys_path = param.getString("path", "");

        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.last(" limit 30 ");

        if (StringKit.isNotBlank(sys_name)) {
            qw.select("name");
            qw.like("name", sys_name);
            qw.groupBy("name");
            return dao.list("sys_post", qw);
        }

        if (StringKit.isNotBlank(sys_path)) {
            qw.select("path");
            qw.like("path", sys_path);
            qw.groupBy("path");
            return dao.list("sys_post", qw);
        }

        return new ArrayList<>();
    }


}
